package com.liuzhu.chaoshikucungunli;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.CheckBox;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.textfield.TextInputEditText;
import com.liuzhu.chaoshikucungunli.db.UserDao;
import com.liuzhu.chaoshikucungunli.model.User;
import com.liuzhu.chaoshikucungunli.utils.SharedPreferencesManager;

public class LoginActivity extends AppCompatActivity {
    private TextInputEditText usernameEdit;
    private TextInputEditText passwordEdit;
    private CheckBox rememberPassword;
    private UserDao userDao;
    private SharedPreferencesManager prefsManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        // 初始化
        initViews();
        userDao = new UserDao(this);
        prefsManager = SharedPreferencesManager.getInstance(this);

        // 如果已经登录，直接进入主页
        if (prefsManager.isLoggedIn()) {
            startMainActivity();
            finish();
            return;
        }

        // 恢复保存的登录信息
        if (prefsManager.isRememberPassword()) {
            usernameEdit.setText(prefsManager.getSavedUsername());
            passwordEdit.setText(prefsManager.getSavedPassword());
            rememberPassword.setChecked(true);
        }

        // 设置点击事件
        setupClickListeners();
    }

    private void initViews() {
        usernameEdit = findViewById(R.id.username_edit);
        passwordEdit = findViewById(R.id.password_edit);
        rememberPassword = findViewById(R.id.remember_password);

        findViewById(R.id.forgot_password).setOnClickListener(v -> {
            // TODO: 实现忘记密码功能
            Toast.makeText(this, "忘记密码功能开发中", Toast.LENGTH_SHORT).show();
        });

        findViewById(R.id.btn_register).setOnClickListener(v -> {
            startActivity(new Intent(this, RegisterActivity.class));
        });
    }

    private void setupClickListeners() {
        findViewById(R.id.btn_login).setOnClickListener(v -> attemptLogin());
    }

    private void attemptLogin() {
        String username = usernameEdit.getText().toString().trim();
        String password = passwordEdit.getText().toString().trim();

        // 验证输入
        if (TextUtils.isEmpty(username)) {
            usernameEdit.setError("请输入用户名");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            passwordEdit.setError("请输入密码");
            return;
        }

        // 验证用户名和密码
        User user = userDao.getUserByUsername(username);
        if (user != null && password.equals(user.getPassword())) {
            // 登录成功
            prefsManager.setLoginStatus(true, user.getId(), user.getUsername());
            
            // 记住密码
            if (rememberPassword.isChecked()) {
                prefsManager.saveLoginCredentials(username, password);
            } else {
                prefsManager.clearLoginCredentials();
            }

            startMainActivity();
            finish();
        } else {
            Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
        }
    }

    private void startMainActivity() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }
} 